package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.ProductCatalog;
import cn.wolfcode.trip.base.mapper.ProductCatalogMapper;
import cn.wolfcode.trip.base.query.NewsQuery;
import cn.wolfcode.trip.base.query.ProductCatalogQuery;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商城目录业务接口实现类
 */
@Service
public class ProductCatalogServiceImpl implements IProductCatalogService {

    @Autowired
    private ProductCatalogMapper productCatalogMapper;

    /**
     * 分页方法
     * @param qo
     * @return
     */
    public PageInfo query(ProductCatalogQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<ProductCatalog> list = productCatalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    /**
     * 新增或修改方法
     * @param
     */
    public void saveOrUpdate(ProductCatalog productCatalog) {
        //判断是否有序号,如果没有就设置最大的序号+1
        if (productCatalog.getSequence() == null) {
            int max = productCatalogMapper.selectMaxSequence(productCatalog.getId());
            productCatalog.setSequence(max + 1);
        }

        if(productCatalog.getId()!=null){
            productCatalogMapper.updateByPrimaryKey(productCatalog);
        }else{
            productCatalogMapper.insert(productCatalog);
        }
    }

    /**
     * 查询所有目录
     * @return
     */
    public List<ProductCatalog> listAll() {
        return productCatalogMapper.selectAll();
    }

    /**
     * 获取所有热门状态的目录
     * @param state
     * @return
     */
    public List<ProductCatalog> ListAll(Integer state) {
        return productCatalogMapper.selectAppAll(state);
    }
}
